// 分类信息
import { topCategory } from '@/api/constants'
import { findAllCatetory } from '@/api/category'
export default {
  namespaced: true,
  state () { // state内的数据已经自动proxy代理了
    return {
      list: topCategory.map(item => ({ name: item })) // 设置默认初始数据,避免白屏
    }
  },
  mutations: {
    setList (state, payload) {
      state.list = payload
    },
    // 定义显示隐藏当前分类的方法
    show (state, id) {
      const currCategory = state.list.find(item => item.id === id)
      currCategory.open = true
    },
    hide (state, id) {
      const currCategory = state.list.find(item => item.id === id)
      currCategory.open = false
    }
  },
  actions: {
    async getList ({ commit }) {
      const data = await findAllCatetory()
      // 给获取的数据添加open属性用来控制弹层,默认false
      data.result.forEach(item => {
        item.open = false
      })
      commit('setList', data.result)
    }
  }
}
